
package com.yimixian.app.user;

import com.yimixian.app.R;
import com.yimixian.app.util.CustomViewUtils;
import com.yimixian.app.util.UiUtils;

import android.content.Context;
import android.graphics.Canvas;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class SettingsItemView extends FrameLayout {

    @InjectView(R.id.icon)
    ImageView mIcon;
    @InjectView(R.id.settings_item_title)
    TextView mTitleView;
    @InjectView(R.id.link_info_text)
    TextView mLinkInfoView;
    @InjectView(R.id.right_arrow)
    ImageView mRightArrow;
    @InjectView(R.id.alert_badge)
    View mAlertBadge;

    private boolean mDrawDividerBelow;

    public SettingsItemView(Context context) {
        super(context);
        initSettingsItemView();
    }

    public SettingsItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initSettingsItemView();
    }

    public SettingsItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initSettingsItemView();
    }

    private void initSettingsItemView() {
        LayoutInflater.from(getContext()).inflate(R.layout.settings_item_view, this);
        CustomViewUtils.setViewBackground(this,
                getResources().getDrawable(R.drawable.settings_item_bg));
        ButterKnife.inject(this);
    }

    public void setDrawDividerBelow(boolean drawDividerBelow) {
        mDrawDividerBelow = drawDividerBelow;
    }

    public void setStandardViews(int resId, SpannableStringBuilder title, boolean showRightArrow,
            boolean showBadge,
            String linkInfoText) {
        mIcon.setImageResource(resId);
        mTitleView.setText(title);
        mRightArrow.setVisibility(showRightArrow ? VISIBLE : GONE);
        mAlertBadge.setVisibility(showBadge ? VISIBLE : GONE);
        mLinkInfoView.setText(linkInfoText);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int shortDividerMargin = getResources().getDimensionPixelSize(
                R.dimen.settings_item_left_margin);
        if (mDrawDividerBelow) {
            canvas.drawRect(
                    shortDividerMargin,
                    getHeight() - 1,
                    getWidth(),
                    getHeight(),
                    UiUtils.getDividerPaint());
        }
    }
}
